package com.epic.engine.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class DBUtils {
    static Connection conn = null;
    static Statement statement = null;
    static String url;
    static String dbUser;
    static String dbPwd;
    public static String host;

    protected static final Logger logger = LoggerFactory.getLogger(DBUtils.class);

    static {
        Properties properties = new Properties();
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new FileReader("/opt/epic/soc/conf/service/jdbc.properties"));
            properties.load(bufferedReader);
            url = properties.getProperty("jdbc.url");
            dbUser = properties.getProperty("jdbc.username");
            dbPwd = properties.getProperty("jdbc.password");
            host = properties.getProperty("kafka.host");
            Class.forName("org.postgresql.Driver");
            conn = DriverManager.getConnection(url, dbUser, dbPwd);
            statement = conn.createStatement();
        } catch (Exception e) {
            logger.error("pg connnettion init fail.", e);
        }
    }
    public static String getData(){
        String ret = "suc";
        try{
            ResultSet resultSet =  statement.executeQuery("select 1;");
            while (resultSet.next()){
                logger.info(resultSet.getString("name"));
            }
            resultSet.close();
            ret = "ok";
        }catch (Exception e){
            logger.error(e.getMessage());
        }

        return ret;
    }

    public static void close(){
        if (conn != null){
            try{
                statement.close();
                conn.close();
            }catch (Exception e){
                logger.error(e.getMessage());
            }
        }
    }

}
